package sort;

import org.junit.Test;

public class QuickSort {
	@Test
	public void fun() {
		int[] list = {0, 1, 2, 3, 1, 4, 2};
		quickSort(0, list.length - 1, list);
		for(int i : list) {
			System.out.print(i + "\t");
		}
	}
	public void quickSort(int left, int right, int[] list) {
		//选枢轴进行划分
		if(left < right) {
			int i = left;
			int j = right + 1;
			int privot = list[left]; //定义一个枢轴
			do{
				do{
					i ++;
				}while(list[i] < privot);//找到一个比枢轴大的数
				do{
					--j;
				}while(list[j] > privot);//找到一个比枢轴小的数
				if(i<j) swap(i, j, list);
			}while(i < j);
			
			swap(left, j, list);	//将基准数放置到中间
			
			quickSort(left, j - 1, list);
			quickSort(j + 1, right, list);
		}
	}
	public void swap(int i, int j, int[]list) {
		int temp = list[i];
		list[i] = list[j];
		list[j] = temp;
	}
}
